package simulation;

import streaming.SourceHost;
import streaming.StreamingConfig;

public class PeriodicPropagate implements EventHandler{

	SourceHost source;
	Simulator sim;
	
	public PeriodicPropagate(SourceHost newSource){
		this.setSource(newSource);
		this.setSim(Simulator.getInstance());
	}
	
	public void addEvent() {
		double time = this.getSim().getTime() + this.getInterval();
		
		if (time <= Config.TIME){ // Do not create new propagate events after end of transmission
			if (Math.abs(time - Math.round(time)) < 0.00001)
				time = Math.round(time);
			this.getSim().addEvent(time, this);
		}
	}
	
	public void run(double time){
		this.getSource().propagate();
	}

	/* Getters and Setters */
	
	public SourceHost getSource() {
		return source;
	}

	public void setSource(SourceHost source) {
		this.source = source;
	}

	public Simulator getSim() {
		return sim;
	}

	public void setSim(Simulator sim) {
		this.sim = sim;
	}

	public double getInterval() {
		return 1 / (StreamingConfig.MCASTRATE * 1.0);
	}
}
